<!--
 * @Author: YaYa
 * @Date: 2022-10-12 14:11:15
 * @LastEditTime: 2022-10-18 23:28:44
 * @LastEditors: YaYa
 * @Description:
 * @FilePath: \HeyTea-ui\src\pages\admin\custome\List.vue
 * 别乱动！
-->
<template>
  <!-- 顾客管理 -->
  <div>
    <!-- 第一行表单控件开始 -->
    <el-form :inline="true" :model="params" class="demo - form - inline">
      <el-form-item>
        <el-button type="primary" @click="toAdd">添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.username" placeholder="用户名" clearable />
      </el-form-item>
      <el-form-item>
        <el-select v-model="params.status" placeholder="请选择状态">
          <el-option label="禁用" value="禁用" />
          <el-option label="正常" value="正常" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 第一行表单控件结束 -->
    <!-- 第二行表单开始 -->
    <el-table :data="searchCustoms" style="width: 100%">
      <el-table-column prop="id" label="编号" width="180" />
      <el-table-column prop="username" label="用户名" width="180" />
      <el-table-column prop="realname" label="姓名" />
      <el-table-column prop="telephone" label="手机号" width="180" />
      <el-table-column prop="gender" label="性别" width="180" />
      <el-table-column prop="status" label="状态" width="180">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '正常'" type="success">
            正常
          </el-tag>
          <el-tag v-if="scope.row.status === '禁用'" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="date" label="操作" width="180">
        <template slot-scope="scope">
          <a class="toEdit" @click="toEdit(scope.row)">编辑</a>
          <a class="toDelete" @click="toDelete(scope.row.id)">删除</a>
          <a class="toDetails" @click="toDetails(scope.row)">详情</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 第二行表单结束 -->
    <!-- 第三行分页查询开始 -->
    <el-pagination
      :current-page="params.page"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="params.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 第三行分页查询结束 -->
    <!-- 添加和编辑模态框开始 -->
    <el-dialog :title="title" :visible.sync="visible" @close="resetForm">
      <el-form ref="myForm" :model="form" :rules="rules">
        <el-form-item
          label="用户名"
          prop="username"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="姓名"
          prop="realname"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.realname" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="手机号"
          prop="telephone"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.telephone" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.gender">
            <el-radio label="male">男</el-radio>
            <el-radio label="female">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加和编辑模态框结束 -->
  </div>
</template>

<script>
import { get, post } from '@/utils/request'
import { pageQuery } from '@/api/user'

export default {
  data() {
    return {
      // thisId: "",
      allCustoms: [],
      searchCustoms: [],
      params: {
        page: 1,
        pageSize: 10,
        roleId: 6
      },
      // 顾客总人数
      total: 0,
      title: '',
      visible: false,
      form: {},
      formLabelWidth: '120px',
      gender: '',
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        telephone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    // this.getAllCustoms();
    this.getpageQuery()
  },
  mounted() {},
  methods: {
    // 清除验证
    resetForm() {
      this.$refs.myForm.resetFields()
      this.imageUrl = ''
    },
    // 跳到详情页面
    toDetails(row) {
      this.$router.push({
        path: '/custome/details',
        query: row
      })
      // console.log(row, "hhhh");
      // Bus.$emit("toDetails", row);
    },
    // 去删除
    async toDelete(id) {
      this.$confirm('此操作将永久删除顾客, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      const res = await get('/baseUser/deleteById', { id })
      console.log(res, '删除')

      this.getpageQuery()
    },
    // 编辑顾客
    toEdit(row) {
      // delete row[registerTime]
      // console.log(row);
      this.form = { ...row }
      this.title = '修改用户信息'
      this.visible = true
    },

    // 提交新增的数据
    toSubmit() {
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          const sum = { ...this.form }
          sum.roleId = 6
          // console.log(sum);
          // let sum = { ...this.form };
          // 发送新增的数据
          if (this.title === '添加顾客信息') {
            const res = await post('/baseUser/addUserWidthRole', sum)
            this.$message({
              type: 'success',
              message: res.message
            })
          } else {
            const res = await post('/baseUser/saveOrUpdate', this.form)
            this.$message({
              type: 'success',
              message: res.message
            })
          }

          // console.log(res, "号");

          this.visible = false
          this.getpageQuery()
        } else {
          this.$message({
            type: 'error',
            message: '请输入正确的形式'
          })
        }
      })
    },
    // 去添加
    toAdd() {
      this.visible = true
      this.title = '添加顾客信息'
      this.form = {}
    },
    // 去查询
    toSearch() {
      this.getpageQuery()
    },
    // // 查询所有顾客
    // async getAllCustoms() {
    //   let res = await get("/baseUser/pageQuery", this.params);

    //   this.allCustoms = res.data.list;
    //   // console.log(res.data.list);
    // },
    // 分页查询顾客
    async getpageQuery() {
      for (const key in this.params) {
        this.loading = true
        if (!this.params[key]) {
          delete this.params[key]
        }
      }
      const res = await pageQuery(this.params)
      // console.log(res, "hhh");
      this.searchCustoms = res.data.list
      this.total = res.data.total
      // 关闭加载动画
      this.loading = false
    },
    // 页码条数发生改变  默认参数pageSize
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize
      this.getpageQuery()
    },
    // 当前页面发生改变触发的方法 默认参数page
    handleCurrentChange(page) {
      this.params.page = page
      this.getpageQuery()
    }
  }
}
</script>
<style scoped>
</style>
